package com.admin.controller;


import com.admin.service.IStationService;
import com.bean.ResponseBean;
import com.util.ApkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *  文件信息
 * @author marisheng
 * @since 2019-11-21
 */
@RestController
@RequestMapping("/admin/fileinfo")
@CrossOrigin("*")
public class FileinfoController {

    @Value("${upload.tmp.dir}")
    private String tmp;

    /**
     * 文件上传到本地硬盘
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "upload/image",method = RequestMethod.POST)
    public ResponseBean upload(MultipartFile file) throws IOException {

        String oldFile = file.getOriginalFilename();//得到旧的文件名
        int index = oldFile.lastIndexOf(".");//截取
        String newFile = oldFile.substring(index);//获取到的是后缀
        String file2=System.currentTimeMillis()+newFile;//最后生成的文件名
        //生成一个真正的图片
        File file1=new File(tmp,file2);
        file.transferTo(file1);
        if(newFile.equals(".apk")){
            //根据路径获取应用信息
            Map map = ApkUtil.getApkMessage(file1.getPath());
            map.put("filePath",file1.getPath());
            return new ResponseBean(0,"应用已上传",map);
        }
        return new ResponseBean(0,"图片已上传",file2);
    }
}
